package databaseVersionControl.domain.db;

import databaseVersionControl.domain.dialect.Dialect;

public class Sequence implements SequenceComponent {
	
	private String name;
	private Integer initialValue;
	private Integer incrementBy;
	private Integer cache;
	
	public Sequence(String name) {
		this.name = name;
	}

	public Sequence initialValue(Integer initialValue) {
		this.initialValue = initialValue;
		return this;
	}
	
	public Sequence incrementBy(Integer incrementBy) {
		this.incrementBy = incrementBy;
		return this;
	}
	
	public Sequence cache(Integer cache) {
		this.cache = cache;
		return this;
	}

	public String getName() {
		return name;
	}
	
	@Override
	public String sqlInDialect(Dialect dialect) {
		StringBuilder stringBuilder = new StringBuilder("SEQUENCE ");
		stringBuilder.append(name);
		
		if (initialValue != null) {
			stringBuilder.append(" START WITH ");
			stringBuilder.append(initialValue);
		}
		
		if (incrementBy != null) {
			stringBuilder.append(" INCREMENT BY ");
			stringBuilder.append(incrementBy);
		}
		
		if (cache != null) {
			stringBuilder.append(" CACHE ");
			stringBuilder.append(cache);
		}
		
		return stringBuilder.toString();
	}

	@Override
	public String sqlIdentifier() {
		return "SEQUENCE " + getName();
	}
	
}